<template>
  <div class="user-home">

    <!-- 轮播图 -->
    <div class="banner-section">
      <el-carousel height="400px" indicator-position="outside">
        <el-carousel-item v-for="item in banners" :key="item.id">
          <div class="banner-item" :style="{ backgroundImage: `url(${item.image})` }">
            <div class="banner-content">
              <h2 class="banner-title">{{ item.title }}</h2>
              <p class="banner-desc">{{ item.description }}</p>
              <el-button size="large" type="warning" round>立即抢购</el-button>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 商品分类导航 -->
    <div class="category-nav">
      <div class="container">
        <el-row :gutter="20" class="category-grid">
          <el-col
            v-for="category in categories"
            :key="category.id"
            :xs="12"
            :sm="8"
            :md="6"
            :lg="3"
            class="category-item"
            @click="goToCategory(category.name)"
          >
            <div class="category-content">
              <div class="category-icon">
                <img :src="category.icon" :alt="category.name" />
              </div>
              <span class="category-name">{{ category.name }}</span>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>

    <!-- 商品展示区域 -->
    <div class="products-section">
      <div class="container">
        <!-- 热销商品 -->
        <el-row class="section-header" justify="space-between" align="middle">
          <el-col>
            <h2 class="section-title">热销推荐</h2>
          </el-col>
          <el-col>
            <el-button type="text" class="more-btn">查看更多 ></el-button>
          </el-col>
        </el-row>

        <el-row class="products-grid">
          <el-col
            v-for="product in hotProducts"
            :key="product.id"
            :span="6"
            class="product-col"
          >
            <div class="product-card" @click="goToProduct(product.id)">
              <div class="product-image">
                <img :src="product.image" :alt="product.name" />
              </div>
              <div class="product-info">
                <h3 class="product-name">{{ product.name }}</h3>
                <p class="product-desc">{{ product.description }}</p>
                <div class="product-price">
                  <span class="current-price">¥{{ product.price }}</span>
                  <span v-if="product.originalPrice" class="original-price">¥{{ product.originalPrice }}</span>
                </div>
                <div class="product-meta">
                  <el-rate v-model="product.rating" disabled size="small" />
                  <span class="sales">月销{{ product.sales }}+</span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>

        <!-- 新品上市 -->
        <el-row class="section-header" justify="space-between" align="middle">
          <el-col>
            <h2 class="section-title">新品上市</h2>
          </el-col>
          <el-col>
            <el-button type="text" class="more-btn">查看更多 ></el-button>
          </el-col>
        </el-row>

        <el-row  class="products-grid">
          <el-col
            v-for="product in newProducts"
            :key="product.id"
            :span="6"
            class="product-col"
          >
            <div class="product-card" @click="goToProduct(product.id)">
              <div class="product-image">
                <img :src="product.image" :alt="product.name" />
                <div class="product-badge new-badge">NEW</div>
              </div>
              <div class="product-info">
                <h3 class="product-name">{{ product.name }}</h3>
                <p class="product-desc">{{ product.description }}</p>
                <div class="product-price">
                  <span class="current-price">¥{{ product.price }}</span>
                  <span v-if="product.originalPrice" class="original-price">¥{{ product.originalPrice }}</span>
                </div>
                <div class="product-meta">
                  <el-rate v-model="product.rating" disabled size="small" />
                  <span class="sales">月销{{ product.sales }}+</span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>

    <!-- 底部信息 -->
    <div class="footer">
      <div class="container">
        <el-row :gutter="30" class="footer-content">
          <el-col :xs="24" :sm="12" :md="6" class="footer-section">
            <h4>服务保障</h4>
            <ul>
              <li>正品保证</li>
              <li>7天退换</li>
              <li>退货返运费</li>
              <li>客户服务</li>
            </ul>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" class="footer-section">
            <h4>支付方式</h4>
            <ul>
              <li>货到付款</li>
              <li>在线支付</li>
              <li>分期付款</li>
              <li>公司转账</li>
            </ul>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" class="footer-section">
            <h4>商家服务</h4>
            <ul>
              <li>广告服务</li>
              <li>京东通信</li>
              <li>京东联盟</li>
              <li>营销中心</li>
            </ul>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6" class="footer-section">
            <h4>关于我们</h4>
            <ul>
              <li>了解我们</li>
              <li>加入我们</li>
              <li>用户协议</li>
              <li>隐私政策</li>
            </ul>
          </el-col>
        </el-row>

        <el-row class="footer-bottom">
          <el-col :span="24">
            <p>&copy; 2024 鞋城商城. All rights reserved. 专业的鞋类购物平台</p>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref,onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { findHotProducts,findNewProducts,findCategories } from '@/api/product'
import {
  ShoppingCart,
  View,
  Star
} from '@element-plus/icons-vue'

const router = useRouter()

//vue挂载完成之后，指定onMonted钩子函数
onMounted(() => {
    FindHotProducts()
    FindNewProducts()
    FindCategories()
})

//查询热门商品
const FindHotProducts = () => {
    findHotProducts().then((res) => {
      console.log('热门商品', res.data);
      hotProducts.value = res.data;
    })
}
//查询新品上市
const FindNewProducts = () => {
    findNewProducts().then((res) => {
      console.log('新品上市', res.data);
      newProducts.value = res.data;
    })
}
//查询鞋的种类
const FindCategories = () => {
  findCategories().then((res) => {
    console.log('鞋的种类', res.data);
    categories.value = res.data.slice(0, 8);
  })
}

const banners = ref([
  {
    id: 1,
    title: '春季新品大促销',
    description: '精选春季新款鞋履，限时特惠，不容错过！',
    image: ''
  },
  {
    id: 2,
    title: '运动鞋专场',
    description: '专业运动鞋品牌汇聚，让运动更精彩',
    image: ''
  },
  {
    id: 3,
    title: '时尚女鞋节',
    description: '优雅时尚，彰显女性魅力',
    image: ''
  }
])

// 鞋的种类
const categories = ref([
])

// 热门商品
const hotProducts = ref([

])

// 新品上市
const newProducts = ref([
])

// 跳转分类
const goToCategory = (keyword) => {
  router.push({ 
    path: "/search",
    query: {keyword}
   })
}

// 跳转商品详情
const goToProduct = (productId) => {
  router.push({ 
    path: "/product/detail",
    query: {productId}
   })
}
</script>

<style scoped>
.user-home {
  min-height: 100vh;
  background-color: #f9f9f9;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Header Styles */
.header {
  background: linear-gradient(135deg, #ff8c00 0%, #ffa500 100%);
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.header-top {
  padding: 15px 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.brand-name {
  font-size: 24px;
  font-weight: bold;
  color: white;
}

.search-section {
  display: flex;
  justify-content: center;
}

.search-input {
  width: 100%;
  max-width: 400px;
}

.user-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 15px;
}

.cart-btn {
  background-color: rgba(255,255,255,0.2);
  border-color: rgba(255,255,255,0.3);
  color: white;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: white;
  cursor: pointer;
  font-size: 14px;
}

.nav-menu {
  padding-bottom: 0;
}

.main-menu {
  border: none;
  justify-content: center;
}

/* Banner Styles */
.banner-section {
  margin-bottom: 40px;
}

.banner-item {
  height: 400px;
  background-size: cover;
  background-position: center;
  background-color: #ff8c00;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.banner-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.3);
}

.banner-content {
  text-align: center;
  color: white;
  z-index: 1;
}

.banner-title {
  font-size: 36px;
  font-weight: bold;
  margin-bottom: 16px;
}

.banner-desc {
  font-size: 18px;
  margin-bottom: 24px;
}

/* Category Navigation */
.category-nav {
  background: white;
  padding: 30px 0;
  margin-bottom: 40px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.category-item {
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 20px;
}

.category-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px;
  border-radius: 8px;
  transition: all 0.3s;
}

.category-content:hover {
  background-color: #fff4e6;
  transform: translateY(-2px);
}

.category-icon img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f0f0f0;
}

.category-name {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

/* Products Section */
.products-section {
  background: white;
  padding: 40px 0;
  margin-bottom: 40px;
}

.section-header {
  margin-bottom: 30px;
}

.section-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.more-btn {
  color: #ff8c00;
  font-size: 14px;
}

.products-grid {
  margin-bottom: 60px;
}

.product-col {
  margin-bottom: 24px;
}

.product-card {
  background: rgb(250, 250, 250);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: all 0.3s;
  cursor: pointer;
  height: 100%;
  width: 90%;
  margin: 0 auto;
}

.product-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0,0,0,0.15);
}

.product-image {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-color: #f0f0f0;
}

.product-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
}

.new-badge {
  background-color: #e74c3c;
  color: white;
}

.product-actions {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.3s;
}

.product-card:hover .product-actions {
  opacity: 1;
}

.product-info {
  padding: 16px;
}

.product-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 12px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.current-price {
  font-size: 18px;
  font-weight: bold;
  color: #e74c3c;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sales {
  font-size: 12px;
  color: #999;
}

/* Footer */
.footer {
  background-color: #2c3e50;
  color: #bdc3c7;
  padding: 40px 0 20px;
}

.footer-content {
  margin-bottom: 30px;
}

.footer-section h4 {
  color: white;
  font-size: 16px;
  margin-bottom: 16px;
}

.footer-section ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.footer-section li {
  padding: 6px 0;
  cursor: pointer;
  transition: color 0.3s;
}

.footer-section li:hover {
  color: #ff8c00;
}

.footer-bottom {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #34495e;
}

.footer-bottom p {
  margin: 0;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-top {
    flex-direction: column;
    gap: 15px;
  }

  .products-grid {
    margin-bottom: 30px;
  }
}
</style>